
public class db_bytes {
	private byte[] bData = null;
	private int iPos = 0;
	
	db_bytes (int iSize) {
		bData = new byte[iSize];
	}
	db_bytes () {
		bData = new byte[db_util.DB_PAGE_SIZE];
	}
	db_bytes (byte[] bBytes) {
		bData = bBytes;
	}
	public void write(byte[] bDataBlob) {
		System.arraycopy(bDataBlob, 0, bData, iPos, bDataBlob.length);
		iPos += bDataBlob.length;
	}
	public void seek(int iPosition){
		iPos = iPosition;
	}
	public void write(String sValue) {
		System.arraycopy(intToByteArray(sValue.length()), 0, bData, iPos, 4);
		iPos += 4;
		System.arraycopy(sValue.getBytes(), 0, bData, iPos, sValue.length());
		iPos += sValue.length();
	}
	public void write(String sValue, long lPad) {
		System.arraycopy(intToByteArray(sValue.length()), 0, bData, iPos, 4);
		iPos += 4;
		System.arraycopy(sValue.getBytes(), 0, bData, iPos, sValue.length());
		iPos += lPad;
	}
	public void write(int iVal) {
		System.arraycopy(intToByteArray(iVal), 0, bData, iPos, 4);
		iPos += 4;
	}
	public void write(long lVal) {
		System.arraycopy(longToByteArray(lVal), 0, bData, iPos, 8);
		iPos += 8;
	}
	public byte[] readBytes(int iLength) {
		byte[] oBytes = new byte[iLength];
		System.arraycopy(bData, iPos, oBytes, 0, iLength);
		iPos += iLength;
		return oBytes;
	}
	public int readInt() {
		byte[] bInt = new byte[4];
		System.arraycopy(bData, iPos, bInt, 0, 4);
		iPos += 4;
		return byteArrayToInt(bInt);
	}
	
	public long readLong() {
		byte[] bLong = new byte[8];
		System.arraycopy(bData, iPos, bLong, 0, 8);
		iPos += 8;
		return byteArrayToLong(bLong);
	}
	public int getPointer() {
		return iPos;
	}
	
	public String readStr() {
		int iStrLen = readInt();
		byte[] bString = new byte[iStrLen];
		System.arraycopy(bData, iPos, bString, 0, iStrLen);
		iPos += iStrLen;
		return new String(bString);
	}
	public String readString(int iLength) {
		byte[] bString = new byte[iLength];
		System.arraycopy(bData, iPos, bString, 0, iLength);
		iPos += iLength;
		return new String(bString);
	}
	
	public byte[] getBytes() {
		return bData;
	}
	public static final int byteArrayToInt(byte [] b) {
		return (b[0] << 24)
		+ ((b[1] & 0xFF) << 16)
		+ ((b[2] & 0xFF) << 8)
		+ (b[3] & 0xFF);
	}
	public static final byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
	}
	
	public static final long byteArrayToLong(byte [] b) {
		return ((long)b[0] << 56)
		+ (((long)b[1] & 0xFF) << 48)
		+ (((long)b[2] & 0xFF) << 40)
		+ (((long)b[3] & 0xFF) << 32)
		+ (((long)b[4] & 0xFF) << 24)
		+ (((long)b[5] & 0xFF) << 16)
		+ (((long)b[6] & 0xFF) << 8)
		+ ((long)b[7] & 0xFF);
	}
	public static final byte[] longToByteArray(long value) {
        return new byte[] {
                (byte)(value >>> 56),
                (byte)(value >>> 48),
                (byte)(value >>> 40),
                (byte)(value >>> 32),
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
	}
	
}
